package com.lin.lazybone.ui;

import android.os.Bundle;
import android.text.InputFilter;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lin.lazybone.R;
import com.lin.lazybone.application.BaseActivity;
import com.lin.lazybone.entity._User;
import com.lin.lazybone.utils.L;
import com.lin.lazybone.utils.ShareUtils;
import com.lin.lazybone.utils.StaticClass;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FetchUserInfoListener;
import cn.bmob.v3.listener.UpdateListener;


/**
 * Created by mr.lin on 2017/11/11.
 */

public class PerDataActicity extends BaseActivity implements View.OnClickListener
{
    private LinearLayout dataset_touxiang;
    private LinearLayout dataset_name;
    private LinearLayout dataset_signature;
    private LinearLayout dataset_sex;
    private LinearLayout dataset_address;
    private TextView dataset_item_phoneNumber;
    private TextView dataset_item_name;
    private TextView dataset_item_signature;
    private TextView dataset_item_sex;
    private TextView dataset_item_address;

    private CustomDialog dialog;
    private CustomDialog sexDialog;
    private EditText perdata_dialog_et;

    private _User user;

    //用来判断是由那个按钮产生的dialog, 0是修改名字，1是修改简介，2是修改收货地址
    private int dialogBtType = 0;


    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_perdata);

        initView();
        initData();


    }

    private void initData()
    {
        user = BmobUser.getCurrentUser(_User.class);
        dataset_item_name.setText(user.getUser_nickname());
        dataset_item_signature.setText(user.getUser_signature());
        dataset_item_sex.setText(user.getUser_sex());
        dataset_item_address.setText(user.getUser_address());
    }

    private void initView()
    {
        dataset_touxiang = (LinearLayout) findViewById(R.id.dataset_touxiang);
        dataset_touxiang.setOnClickListener(this);
        dataset_name = (LinearLayout) findViewById(R.id.dataset_name);
        dataset_name.setOnClickListener(this);
        dataset_signature = (LinearLayout) findViewById(R.id.dataset_signature);
        dataset_signature.setOnClickListener(this);
        dataset_sex = (LinearLayout) findViewById(R.id.dataset_sex);
        dataset_sex.setOnClickListener(this);
        dataset_address = (LinearLayout) findViewById(R.id.dataset_address);
        dataset_address.setOnClickListener(this);

        dataset_item_address = (TextView) findViewById(R.id.dataset_item_address);
        dataset_item_phoneNumber = (TextView) findViewById(R.id.dataset_item_phoneNumber);
        dataset_item_name = (TextView) findViewById(R.id.dataset_item_name);
        dataset_item_signature = (TextView) findViewById(R.id.dataset_item_signature);
        dataset_item_sex = (TextView) findViewById(R.id.dataset_item_sex);

        dialog = new CustomDialog(this, 0, 1000, R.layout.dialog_perdata,
                R.style.Theme_dialog, Gravity.CENTER, R.style.pop_anim_style);
        Button bt = (Button) dialog.findViewById(R.id.perdata_dialog_bt);
        bt.setOnClickListener(this);
        perdata_dialog_et = (EditText) dialog.findViewById(R.id.perdata_dialog_et);

        sexDialog = new CustomDialog(this, 0, 500, R.layout.dialog_sex,
                R.style.Theme_dialog, Gravity.BOTTOM, R.style.pop_anim_style);
        Button sexman = (Button) sexDialog.findViewById(R.id.perdata_dialog_man);
        sexman.setOnClickListener(this);
        Button sexwoman = (Button) sexDialog.findViewById(R.id.perdata_dialog_woman);
        sexwoman.setOnClickListener(this);

    }


    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.dataset_touxiang:
                Toast.makeText(this, "touxiang", Toast.LENGTH_SHORT).show();
                break;
            case R.id.dataset_name:
                dialogBtType = 0;
                perdata_dialog_et.setFilters(new InputFilter[]{new InputFilter.LengthFilter(6)});
                perdata_dialog_et.setHint("最大长度6个字符");
                perdata_dialog_et.setText(dataset_item_name.getText());
                dialog.show();
                break;
            case R.id.dataset_signature:
                dialogBtType = 1;
                perdata_dialog_et.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)});
                perdata_dialog_et.setHint("最大长度20个字符");
                perdata_dialog_et.setText(dataset_item_signature.getText());
                dialog.show();
                break;
            case R.id.dataset_sex:
                sexDialog.show();
                break;
            case R.id.dataset_address:
                dialogBtType = 2;
                perdata_dialog_et.setFilters(new InputFilter[]{new InputFilter.LengthFilter(30)});
                perdata_dialog_et.setHint("最大长度30个字符");
                perdata_dialog_et.setText(dataset_item_address.getText());
                dialog.show();
                break;
            case R.id.perdata_dialog_man:
                sexDialog.dismiss();
                updateSex("男");
                break;
            case R.id.perdata_dialog_woman:
                sexDialog.dismiss();
                updateSex("女");
                break;
            case R.id.perdata_dialog_bt:
                //确认修改资料
                _User userUpdate = new _User();
                userUpdate.setSessionToken(user.getSessionToken());
                final String data = perdata_dialog_et.getText().toString().trim();
                if (dialogBtType == 0) userUpdate.setUser_nickname(data);
                if (dialogBtType == 1) userUpdate.setUser_signature(data);
                if (dialogBtType == 2) userUpdate.setUer_address(data);
                userUpdate.update(user.getObjectId(), new UpdateListener() {
                    @Override
                    public void done(BmobException e) {
                        if(e==null){
                            Toast.makeText(PerDataActicity.this, "更新成功", Toast.LENGTH_SHORT).show();
                            if (dialogBtType == 0) dataset_item_name.setText(data);
                            if (dialogBtType == 1) dataset_item_signature.setText(data);
                            if (dialogBtType == 2) dataset_item_address.setText(data);
                        }else{
                            Toast.makeText(PerDataActicity.this, "更新失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                perdata_dialog_et.setText("");
                dialog.dismiss();
                break;

        }
    }

    private void updateSex(final String sex)
    {
        _User userUpdate = new _User();
        userUpdate.setUser_sex(sex);
        userUpdate.setSessionToken(user.getSessionToken());
        userUpdate.update(user.getObjectId(), new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if(e==null){
                    Toast.makeText(PerDataActicity.this, "更新成功", Toast.LENGTH_SHORT).show();
                    dataset_item_sex.setText(sex);
                }else{
                    Toast.makeText(PerDataActicity.this, user.getObjectId() + "更新失败", Toast.LENGTH_SHORT).show();
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        BmobUser.fetchUserJsonInfo(new FetchUserInfoListener<String>() {
            @Override
            public void done(String s, BmobException e) {
//                if (e == null) {
//                    Toast.makeText(PerDataActicity.this, "获取最新资料成功", Toast.LENGTH_SHORT).show();
//                } else {
//                    Toast.makeText(PerDataActicity.this, "获取最新资料失败", Toast.LENGTH_SHORT).show();
//                }
            }
        });
    }
}
